/**
 * 
 */
package cn.sundat.web;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.Servlet;

import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.sundat.model.PageBean;
import cn.sundat.model.School;
import cn.sundat.model.School_link;
import cn.sundat.service.SchoolService;

/**
 * @author XIN
 *
 */
public class SchoolAction extends ActionSupport implements ModelDriven<School>{
	
	
	@Resource(name="schoolService")
	private SchoolService schoolService;
	School school=new School();
	@Override
	public School getModel() {
		// TODO Auto-generated method stub
		return school;
	}
	public String add() throws Exception {
		// TODO Auto-generated method stub
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			schoolService.addSchool(school);
			writer.print("1");
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}finally {
			writer.flush();
			writer.close();
		}
		
		return NONE;
	}
	public String del() throws Exception {
		// TODO Auto-generated method stub
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			schoolService.delSchool(schoolService.findSchoolById(ServletActionContext.getRequest().getParameter("uuid")));
			writer.print("1");
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}finally {
			writer.flush();
			writer.close();
		}
		return NONE;
	}
	public String edit() throws Exception{
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			schoolService.editSchool(school);
			writer.print("1");
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}finally {
			writer.flush();
			writer.close();
		}
		return NONE;
	}
	public String findbyid() throws Exception{
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			School school= schoolService.findSchoolById(ServletActionContext.getRequest().getParameter("uuid"));
			String jsonString = JSON.toJSONString(school);
			writer.print(jsonString);
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}finally {
			writer.flush();
			writer.close();
		}
		return NONE;
	}
	
	public String list() throws Exception {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		String currPage = ServletActionContext.getRequest().getParameter("currPage");
		String key = ServletActionContext.getRequest().getParameter("kw");
		if (key==null) {
			key="";
		}
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			PageBean<School> schoolList = schoolService.findByPageByKey(Integer.parseInt(currPage), 2,key);
			String jsonString = JSON.toJSONString(schoolList,SerializerFeature.DisableCircularReferenceDetect);
			writer.print(jsonString);
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}finally {
			writer.flush();
			writer.close();
		}
		return NONE;
	}

}
